package com.costaccounting.web.controller.feesys;

import java.util.List;

import com.costaccounting.common.config.RuoYiConfig;
import com.costaccounting.common.utils.SecurityUtils;
import com.costaccounting.common.utils.file.FileUploadUtils;
import com.costaccounting.framework.config.ServerConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.costaccounting.common.annotation.Log;
import com.costaccounting.common.core.controller.BaseController;
import com.costaccounting.common.core.domain.AjaxResult;
import com.costaccounting.common.enums.BusinessType;
import com.costaccounting.feesys.domain.ZsFile;
import com.costaccounting.feesys.service.IZsFileService;
import com.costaccounting.common.utils.poi.ExcelUtil;
import com.costaccounting.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 附件管理Controller
 * 
 * @author ruoyi
 * @date 2021-11-22
 */
@RestController
@RequestMapping("/feesys/fileinfo")
public class ZsFileController extends BaseController
{
    @Autowired
    private IZsFileService zsFileService;

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询附件管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ZsFile zsFile)
    {
        startPage();
        List<ZsFile> list = zsFileService.selectZsFileList(zsFile);
        return getDataTable(list);
    }

    /**
     * 导出附件管理列表
     */
    @Log(title = "附件管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ZsFile zsFile)
    {
        List<ZsFile> list = zsFileService.selectZsFileList(zsFile);
        ExcelUtil<ZsFile> util = new ExcelUtil<ZsFile>(ZsFile.class);
        return util.exportExcel(list, "附件管理数据");
    }

    /**
     * 获取附件管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(zsFileService.selectZsFileById(id));
    }

    /**
     * 新增附件管理
     */
    @Log(title = "附件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZsFile zsFile)
    {
        return toAjax(zsFileService.insertZsFile(zsFile));
    }

    /**
     * 修改附件管理
     */
    @Log(title = "附件管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZsFile zsFile)
    {
        return toAjax(zsFileService.updateZsFile(zsFile));
    }

    /**
     * 删除附件管理
     */
    @Log(title = "附件管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zsFileService.deleteZsFileByIds(ids));
    }

    /**
     * 通用上传请求
     */
    @PostMapping("/upload/{feeId}")
    public AjaxResult uploadFile(@PathVariable("feeId") Long feeId, MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            ZsFile zsFile=new ZsFile();
            zsFile.setFilePath(url);
            zsFile.setFileName(file.getOriginalFilename());
            zsFile.setCreateBy(SecurityUtils.getUsername());
            zsFile.setFeeId(feeId);
            zsFileService.insertZsFile(zsFile);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/uploadFileDelete/{id}")
    public AjaxResult uploadFileDelete(@PathVariable("id") Long id) throws Exception
    {
        try
        {
           zsFileService.deleteZsFileById(id);
            return AjaxResult.success("删除成功");
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}
